<?php
/**
 * Created by PhpStorm.
 * User: Hong
 * Date: 2018/3/19
 * Time: 9:50
 * Function:
 */

namespace Platform\Models\Organize;

use Illuminate\Database\Eloquent\Model;
use Platform\Models\Traits\ModelTree;

class LeaderMenu extends Model
{
    use ModelTree;

    protected $table = 'leader_menu';
    protected $guarded = [];

    /**
     * 绑定权限表关系
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function permission()
    {
        return $this->hasOne(LeaderPermission::class, 'id', 'permission_id');
    }

    public function setInfoAttribute($value)
    {
        $this->attributes['info'] = serialize($value);
    }

    public function getInfoAttribute($value)
    {
        return unserialize($value);
    }

    /**
     * 继承于 ModelTree ，拼接菜单时处理展示数据
     * @param $node
     * @return array
     */
    public function filterNested($node)
    {
        $node = [
            'id' => $node['id'],
            'parent_id' => $node['parent_id'],
            'permission_uri' => $node['permission_uri'],
            'path' => $node['info']['path'],
            'name' => isset($node['info']['name']) ? $node['info']['name'] : '',
            'component' => $node['info']['component'],
            'meta' => [
                'title' => $node['info']['title'],
                'icon' => $node['info']['icon'],
            ],
        ];
        return $node;
    }
}